/**
 * Created by pujingliang on 16/11/21.
 */

(function($) {

    //获取页面间的url的参数
    (function ($) {
        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
    })(jQuery);

    var contact_arr     = [];

    $(function () {
        initpart();
        //点击确定
        $(".navigation-rightBtn").on('click',function(){

        });
    });

    //获取部门列表
    function initpart(){
        var url_c  = tk_apiurl+"contacteam/mydepts";
        var data = {"userId":user_id,"token":token,"data":JSON.stringify({'teamId':group_id,'parentId':"0"})};
        $.ajax({
            beforeSend:function(){
                $(".loading").show();
            },
            async: true,
            crossDomain: true,
            url: url_c,
            method: "POST",
            data:data,
            jsonpCallback:"jsoncallback",
            dataType: "jsonp",
            success:function(d){
                var data='';
                var data1='';
                if(typeof d == "object"){

                }
                else {
                    data = eval('('+d+')');
                    if(data.result){
                        data1 = eval('(' + data.result + ')');
                    }
                }
                $(".loading").hide();

                var team = data1.depts;
                for(var i=0;i<team.length;i++){
                    var html="<div class='team_bigest' id='"+team[i].id+"'>" +
                        "<div class='team_mid' is_choice='0'>" +
                        "<div class='team_left'>" +
                        "<img src='../../../approval/approvalImg/team.png' class='team_pic'>" +
                        "<div class='team_name'>"+team[i].departmentName+"</div>" +
                        "</div>" +
                        "<div class='team_right'>" +
                        "<img src='../../../log/logImg/rightIcon.png' class='right_icon'></div></div>" +
                        "<div class='contact_content'></div></div>";
                    $(".team_content_bigest").append(html);
                }
            }
        });
    }
    //点击行获取部门下的成员列表
    var _team;
    $(document).on('click','.team_mid',function(){
        $(this).css("borderBottom","1px solid #e5e5e5");
        _team = $(this).parent();
        var is_choice = $(this).attr("is_choice");
        if(is_choice=="0"){
            var deptId = $(_team).attr("id");
            var url    = tk_apiurl+"contacteam/mydeptsall";
            var data   = {"userId":user_id,"token":token,"data":JSON.stringify({'teamId':group_id,'deptId':deptId})};
            $.ajax({
                beforeSend:function(){

                },
                async: true,
                crossDomain: true,
                url: url,
                method: "POST",
                data:data,
                jsonpCallback:"jsoncallback",
                dataType: "jsonp",
                success:function(d){
                    var data='';
                    var data1='';
                    if(typeof d == "object"){

                    }
                    else {
                        data = eval('('+d+')');
                        if(data.result){
                            data1 = eval('(' + data.result + ')');
                        }
                    }

                    var users = data1.users;

                    var html="";

                    for(var i=0;i<users.length;i++){
                        var _choice = "0";
                        var _img    = "<img src='../../../log/logImg/tick.png' class='tick_icon'>";
                        for(var j=0;j<contact_arr.length;j++){
                            if(contact_arr[j]==users[i].userId){
                                _choice = "1";
                                _img    = "<img src='../../../log/logImg/tick.png' class='tick_icon' style='display: block'>";
                            }
                        }
                        html +="<div class='user_mid' id='"+users[i].userId+"' is_choice='"+_choice+"'>" +
                            "<div class='user_left'>" +
                            "<img src='"+users[i].avatar+"' class='user_pic'>" +
                            "<div class='user_name'>"+users[i].userName+"</div>" +
                            "</div>" +
                            "<div class='user_right'>" +
                            ""+_img+"</div></div>";
                    }
                    $(_team).append(html);

                    $(".user_mid").last().css("border","none");
                }
            });
            $(this).attr("is_choice","1");
        }else{
            $(this).nextAll().hide();
            $(this).attr("is_choice","0");
        }
    });

    //选择联系人
    $(document).on('click','.user_mid',function(){
        var id        = $(this).attr('id');
        var is_choice = $(this).attr("is_choice");

        if(is_choice=="0"){
            $($(this).children(1)[1]).children(0).show();
            $(this).attr("is_choice","1");
            contact_arr.push(id);
        }else{
            $($(this).children(1)[1]).children(0).hide();
            $(this).attr("is_choice","0");
            for(key in contact_arr){
                if(contact_arr[key]==id){
                    contact_arr.splice(key,1);
                }
            }
        }
    });

})(jQuery);